﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using Skynet.BusinessEntities;
using Microsoft.Practices.Unity;

namespace Skynet.BusinessLogic.BusinessComponents
{
    public enum CustomerFindOptions
    {
        GetOne, 
        GetAll,
        GetByUserName
    }
    public enum CustomerSearchOption
    {
        GetAll
 
    }



    [ServiceContract]
    public interface IBCCustomers 
    {
        [OperationContract]
        void MaintenanceCustomers(Customer item);
        [OperationContract]
        IEnumerable<Customer> FindCustomers(CustomerFindOptions option = CustomerFindOptions.GetAll, Guid? id = null, string Name = null, string UserName = null);

        [OperationContract]
        string SearchCustomers(CustomerSearchOption option = CustomerSearchOption.GetAll, Guid? id = null, string Name = null, string UserName = null); 

    }
    public class BCCustomers :  IBCCustomers
    {
        [Dependency]
        public DataAccess.ICustomerRepository CustomerRepository { get; set; }

        public void MaintenanceCustomers(Customer item)
        {
            switch (item.ChangeTracker.State)
            {
                case ObjectState.Unchanged:
                    break;
                case ObjectState.Added:
                    this.CustomerRepository.Add(item);
                    break;
                case ObjectState.Modified:
                    this.CustomerRepository.Update(item);
                    break;
                case ObjectState.Deleted:
                    this.CustomerRepository.Delete(item);  
                    break;
                default:
                    break;
            }
            item.AcceptChanges();  
        }

        public IEnumerable<Customer> FindCustomers(CustomerFindOptions option = CustomerFindOptions.GetAll, Guid? id = null, string Name = null, string UserName = null)
        {
            IEnumerable<Customer> result = null;

            switch (option)
            {
                case CustomerFindOptions.GetOne:
                    var cus = this.CustomerRepository.GetOne(id); 
                    if ( cus != null) 
                    {
                      result = new List<Customer>(){cus};
                    }                    
                    break;
                case CustomerFindOptions.GetAll:
                    result = this.CustomerRepository.GetAll(); 
                    break;
                case CustomerFindOptions.GetByUserName:
                    var cusUserName = this.CustomerRepository.GetOneByUserName(UserName);
                    result = new List<Customer>() { cusUserName };
                    break;
                default:
                    break;
            }
            return result;  
        }
        [OperationContract]
        public  string SearchCustomers(CustomerSearchOption option = CustomerSearchOption.GetAll, Guid? id = null, string Name = null, string UserName = null)
        {
            return null; 
        }

    }
}
